<?php

namespace App\Utils;

class Arr
{
    public static function List2Tree(array $list, string $pk = 'id', string $pPk = 'pid', string $child = 'children'): array
    {
        $tree = [];
        $refer = [];
        foreach ($list as &$item) {
            $refer[$item[$pk]] = &$item;
        }
        foreach ($list as &$data) {
            $pid = $data[$pPk];
            if (isset($refer[$pid])) {
                $p = &$refer[$pid];
                $p[$child][] = &$data;
            } else {
                $tree[] = &$data;
            }
        }
        return $tree;
    }
}
